﻿Imports KanBan.Pgd.Domain.Entities
Imports KanBan.Pgd.Presentation.Helpers
Imports KanBan.Pgd.Infrastructure.Exceptions
Imports KanBan.Pgd.Services

Public Class EditInterview

    Public Event InterviewAdded(ByVal addedInterview As Interview)
    Public Event InterviewUpdated(ByVal updatedInterview As Interview)

    Private app As New PgdApp()

    ''' <summary>
    ''' Property accesor to the shared Search Request.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property SearchRequest() As SearchRequest
        Get
            Return EditSearchRequestContainer.SearchRequest
        End Get
        Set(ByVal value As SearchRequest)
            EditSearchRequestContainer.SearchRequest = value
        End Set
    End Property

    Private _interviewId As Guid
    Public Property InterviewId() As Guid
        Get
            Return _interviewId
        End Get
        Set(ByVal value As Guid)
            _interviewId = value
        End Set
    End Property

    Public Property CandidateId() As Guid
        Get
            Return CType(cmbCandidates.SelectedValue, Guid)
        End Get
        Set(ByVal value As Guid)
            cmbCandidates.SelectedValue = value
        End Set
    End Property

    Public Property DateTime() As DateTime
        Get

            Dim year As Integer = dtpInterviewDate.Value.Year
            Dim month As Integer = dtpInterviewDate.Value.Month
            Dim day As Integer = dtpInterviewDate.Value.Day
            Dim hour As Integer = dtpInterviewHour.Value.Hour
            Dim minute As Integer = dtpInterviewHour.Value.Minute
            Dim second As Integer = dtpInterviewHour.Value.Second

            Return New Date(year, month, day, hour, minute, second)

        End Get
        Set(ByVal value As DateTime)
            dtpInterviewDate.Value = value
            dtpInterviewHour.Value = value
        End Set
    End Property

    Public Property RoomId() As Guid
        Get
            Return CType(cmbRooms.SelectedValue, Guid)
        End Get
        Set(ByVal value As Guid)
            cmbRooms.SelectedValue = value
        End Set
    End Property

    Public Property Comments() As String
        Get
            Return txtComments.Text
        End Get
        Set(ByVal value As String)
            txtComments.Text = value
        End Set
    End Property

    Public Sub New()

        Try

            ' This call is required by the Windows Form Designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.

            Me.LoadSelectableProperties()

        Catch ex As Exception

            FormHelper.ShowError(ExceptionHandler.HandleException(ex))

        End Try

    End Sub

    Public Sub New(ByVal interviewId As Guid)

        Try

            ' This call is required by the Windows Form Designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.

            Me.InterviewId = interviewId

            Me.LoadSelectableProperties()

            Dim interview = Me.SearchRequest.GetInterviewById(interviewId)

            cmbCandidates.SelectedValue = interview.Candidate.Id
            Me.CandidateId = interview.Candidate.Id

            cmbRooms.SelectedValue = interview.Room.Id
            dtpInterviewDate.Value = interview.DateTime
            dtpInterviewHour.Value = interview.DateTime
            txtComments.Text = interview.Comments

        Catch ex As Exception

            FormHelper.ShowError(ExceptionHandler.HandleException(ex))

        End Try

    End Sub

    Private Sub LoadSelectableProperties()

        Dim candidates = Me.SearchRequest.Candidates
        Me.AddCandidates(candidates)

        Dim rooms = app.Rooms.GetAll()
        Me.AddRooms(rooms)

    End Sub

    Private Sub AddCandidates(ByVal candidates As IEnumerable(Of Candidate))

        Dim candidatesList As New List(Of Candidate)(candidates)

        cmbCandidates.DataSource = candidatesList
        cmbCandidates.DisplayMember = "Name"
        cmbCandidates.ValueMember = "Id"

    End Sub

    Private Sub AddRooms(ByVal rooms As IEnumerable(Of Room))

        Dim roomsList As New List(Of Room)(rooms)

        cmbRooms.DataSource = roomsList
        cmbRooms.DisplayMember = "Name"
        cmbRooms.ValueMember = "Id"

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Me.AddOrUpdateInterview()
        Me.Close()
    End Sub

    Private Sub AddOrUpdateInterview()

        Dim candidate = CType(cmbCandidates.SelectedItem, Candidate)
        Dim room = CType(cmbRooms.SelectedItem, Room)

        If _interviewId = Guid.Empty Then 'Add

            Dim interview = New Interview(SearchRequest, _
                                      Me.DateTime, _
                                      candidate, _
                                      room, _
                                      Me.Comments)

            Me.SearchRequest.Interviews.Add(interview)

            RaiseEvent InterviewUpdated(interview)

        Else 'Update

            Dim interview = Me.SearchRequest.GetInterviewById(_interviewId)

            interview.DateTime = Me.DateTime
            interview.Candidate = candidate
            interview.Room = room
            interview.Comments = Me.Comments

            RaiseEvent InterviewAdded(interview)

        End If

    End Sub

End Class